﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UMD.HCIL.Piccolo;
using System.Drawing;
using UMD.HCIL.Piccolo.Nodes;

namespace TreeMap.Graphics
{
	public class ToolTip : PNode
	{
		#region Singleton stuff
		private static ToolTip MyInstance = new ToolTip();

		public static ToolTip GetInstance
		{
			get
			{
				return MyInstance;
			}
		}
		#endregion


		public string Text { get; set; }
		public Brush Brush { get; set; }
		public float Width { get; set; }
		public float Height { get; set; }
		public bool Visible { get; set; }
		public Point Pos { get; set;}

		private PNode PNode;
		private PText PText;
		private PPath PPath;
		

		//Initialize Font to use for PText
		private Font MyFont = new Font("Tahoma", 8);

		/// <summary>
		/// Constructor
		/// </summary>
		private ToolTip()
		{
			Pos = new Point();
			Brush = new System.Drawing.SolidBrush(Color.FromArgb(255, 255, 225));

			PNode = new PNode();
			PText = new PText();
			PPath = new PPath();
			PText.Font = this.MyFont;

			//Color of PNode
			PNode.Brush = Brush;
		}

		/// <summary>
		/// Displays the tooltip in the Canvas
		/// </summary>
		/// <param name="canvas"></param>
		public void ShowToolTip(PCanvas canvas, string text, Point pos)
		{
			//text
			Text = text;
			PText.Text = Text;

			//Width and Height
			PNode.Width = PText.Width;
			PNode.Height = PText.Height;
			
			//PPath.X = PNode.X;
			//PPath.Y = PNode.Y;
			//PPath.Height = PNode.Height;
			//PPath.Width = PNode.Width;
			//PPath.AddRectangle(pos.X, pos.Y, PNode.Width, PNode.Height);

			//Offset
			PText.OffsetX = pos.X;
			PText.OffsetY = pos.Y;
			PNode.OffsetX = pos.X;
			PNode.OffsetY = pos.Y;

			//Color of PNode
			PNode.Brush = Brush;
			PPath.Brush = Brushes.Black;

			//Visibility
			PNode.Visible = Visible;
			PText.Visible = Visible;
			PPath.Visible = Visible;
			
			//Adding to Layer
			//canvas.Layer.AddChild(PPath);
			canvas.Layer.AddChild(PNode);
			canvas.Layer.AddChild(PText);
		}

		/// <summary>
		/// Disables the tooltip from the canvas
		/// </summary>
		/// <param name="canvas"></param>
		public void RemoveToolTip(PCanvas canvas)
		{
			//canvas.Layer.RemoveChild(PPath);
			if (canvas.Layer.AllNodes.Contains(PText))
			{
				canvas.Layer.RemoveChild(PText);
			}
			if (canvas.Layer.AllNodes.Contains(PNode))
			{
				canvas.Layer.RemoveChild(PNode);
			}
			
		}
	}
}
